const { con, Result } = require('./common');
let Pic = async (id, filename) => {
        filename.map(async (item) => {
                let sql2 = `insert into dynamic_pic values(${id},'${item}')`;
                let [rows2] = await con.execute(sql2);
        })
}
let Dynamic = async (userd_id, dynamic_title, dynamic_context, time, filename) => {
        let res = new Result();
        let sql = `select * from dynamic`;
        let [rows] = await con.execute(sql);
        if (rows.length == 0) {
                let sql1 = `insert into dynamic values(1,${userd_id},'${dynamic_title}','${dynamic_context}','${time}')`;
                let [rows1] = await con.execute(sql1);
                if (filename) {
                        Pic(1, filename);
                }
        } else {
                let sql1 = `insert into dynamic values(${rows[rows.length - 1].dynamic_id + 1},${userd_id},'${dynamic_title}','${dynamic_context}','${time}')`;
                let [rows1] = await con.execute(sql1);
                if (filename) {
                        Pic(rows[rows.length - 1].dynamic_id + 1, filename);
                }
        }
        let sql3 = `select * from dynamic where userd_id = ${userd_id}`;
        let [rows3] = await con.execute(sql3);
        res.data = rows3[rows3.length - 1];
        return res;
}
let Dynamic_get_one = async (userd_id) => {
        let res = new Result();
	console.log(userd_id);
        let sql = `select * from dynamic where userd_id = ${userd_id}`;
        let [rows] = await con.execute(sql);
        let pic = new Array();
        let i = 0;
        for (let j = 0; j < rows.length; j++) {
                let sql1 = `select * from dynamic_pic where dynamic_id = ${rows[j].dynamic_id}`;
                let [rows1] = await con.execute(sql1);
                if (rows1.length >= 1) {
                        pic[i] = rows1;
                        i++;
                }
        }
        res.data = { pic, data: rows };
        return res;
}
let Dynamic_get_one2 = async (userd_id) => {
        let res = new Result();
        let sql = `select * from dynamic where userd_id = ${userd_id}`;
	let sql2 = `select user_name,user_pic from user where user_id = ${userd_id}`;
	let [rows3] = await con.execute(sql2);
	console.log(rows3[0].user_name);
	let arr =[];
	class Item {
	  	constructor() {
			this.user_id = userd_id;
    			this.user_name = '';
    			this.user_pic = '';
    			this.dynamic = {};
			this.dynamic_pic = {};
  		}
	}

        let [rows] = await con.execute(sql);
        for (let j = 0; j < rows.length; j++) {
		let item = new Item();
                let sql1 = `select dynamic_pic_url from dynamic_pic where dynamic_id = ${rows[j].dynamic_id}`;
		item.dynamic = rows[j];
		item.dynamic_id = rows[j].dynamic_id;
		item.user_name = rows3[0].user_name;
		item.user_pic = rows3[0].user_pic;
                let [rows1] = await con.execute(sql1);
                if (rows1.length >= 1) {
                        item.dynamic_pic = rows1;
                }
		arr[j] = item;
        }
        res.data = arr;
        return res;
}

let Dynamic_delete = async (dynamic_id) => {
        let res = new Result();
        let sql = `delete from comment where dynamicco_id = ${dynamic_id}`;
        let [rows] = await con.execute(sql);
        let sql2 = `delete from dynamic_pic where dynamic_id = ${dynamic_id}`;
        let [rows2] = await con.execute(sql2);
        let sql1 = `delete from dynamic where dynamic_id = ${dynamic_id}`;
        let [rows1] = await con.execute(sql1);
        return res;
}
let Dynamic_Get = async () => {
        let res = new Result();
        let sql = `select * from dynamic`;
        let [rows] = await con.execute(sql);
	let sql3 = `select dynamic_title,dynamic_context,dynamic_time from dynamic`;
	let [rows3] = await con.execute(sql3);
	let arr = [];
	for(let i = 0;i<rows.length;i++){
	let sql1 = `select user_id,user_name,user_pic from user where user_id = ${rows[i].userd_id}`;
	let [rows1] = await con.execute(sql1);
	arr[i] = rows1[0];
	arr[i].dynamic = rows3[i]; 
	arr[i].dynamic_id = rows[i].dynamic_id;
        let sql2 = `select dynamic_pic_url  from dynamic_pic where dynamic_id = ${rows[i].dynamic_id}`;
        let [rows2] = await con.execute(sql2);
	if(rows2.length > 0){
	arr[i].dynamic_pic = rows2;
	}
	}
        res.data = arr;
        return res;
}
let Dynamic_get = async()=>{
	let res = new Result();
	let sql = `select * from dynamic`;
	let [rows] = await con.execute(sql);
	let sql2 = `select * from dynamic_pic`;
	let [pic] = await con.execute(sql2);
	res.data = {data:rows,pic}
	return res;
}	
module.exports = {Dynamic_get_one2,Dynamic_get,Dynamic,Dynamic_Get,Dynamic_delete,Dynamic_get_one};
